﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;

namespace Lolitemchanger
{
    /// <summary>
    /// Logique d'interaction pour Configuration.xaml
    /// </summary>
    public partial class Configuration : Window
    {
        private bool m_bFirstTime = false;

        public Configuration()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            CGestionIni IniFile = new CGestionIni(Environment.CurrentDirectory + @"\Configuration.ini");
            Image imgBrowse = new Image();
            Image imgTick = new Image();
            Image imgBackup = new Image();
            Image imgRestore = new Image();

            imgBrowse.Source = BitmapFrame.Create(new Uri(Environment.CurrentDirectory + @"\Images\browse.ico", UriKind.RelativeOrAbsolute));
            btn_parcourir.Content = imgBrowse;
            btn_parcourir.ToolTip = "Cliquez ici pour rechercher votre dossier HeroPak_Client.zip";

            imgTick.Source = BitmapFrame.Create(new Uri(Environment.CurrentDirectory + @"\Images\Valider.ico", UriKind.RelativeOrAbsolute));
            btn_valider.Content = imgTick;

            imgBackup.Source = BitmapFrame.Create(new Uri(Environment.CurrentDirectory + @"\Images\backup.ico", UriKind.RelativeOrAbsolute));
            btn_backup.Content = imgBackup;
            btn_backup.ToolTip = "Cliquez ici pour faire une sauvegarde de votre dossier HeroPak_Client.zip";

            imgRestore.Source = BitmapFrame.Create(new Uri(Environment.CurrentDirectory + @"\Images\restore.ico", UriKind.RelativeOrAbsolute));
            btn_restore.Content = imgRestore;
            btn_restore.ToolTip = "Cliquez ici pour retourner a votre configuration originale";

            cbx_langue.Items.Add("Français");
            cbx_langue.Items.Add("Anglais");

            if (IniFile.Exist())
            {
                tbx_path.Text = IniFile.RecupererValeur("Configuration", "PATH");
                switch (IniFile.RecupererValeur("Configuration", "LANGUAGE"))
                {
                    case "FRENCH":
                        cbx_langue.SelectedItem = "Français";
                        break;
                    case "ENGLISH":
                        cbx_langue.SelectedItem = "Anglais";
                        break;
                    default:
                        cbx_langue.SelectedItem = "Français";
                        break;
                }
            }
            else
                m_bFirstTime = true;
        }

        private void btn_parcourir_Click(object sender, RoutedEventArgs e)
        {
            CGestionIni IniFile = new CGestionIni(Environment.CurrentDirectory + @"\Configuration.ini");
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.InitialDirectory = Environment.CurrentDirectory;
            ofd.Filter = "zip files (*.zip)|*.zip|all files (*.*)|*.*";
            ofd.RestoreDirectory = true;

            if (ofd.ShowDialog() == true)
                tbx_path.Text = ofd.FileName;
        }

        private void btn_close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btn_valider_Click(object sender, RoutedEventArgs e)
        {
            CGestionIni IniFile = new CGestionIni(Environment.CurrentDirectory + @"\Configuration.ini");

            IniFile.StockerValeur("Configuration", "PATH", tbx_path.Text);

            switch (cbx_langue.SelectedItem.ToString())
            {
                case "Français":
                    IniFile.StockerValeur("Configuration", "LANGUAGE", "FRENCH");
                    break;
                case "Anglais":
                    IniFile.StockerValeur("Configuration", "LANGUAGE", "ENGLISH");
                    break;
                default:
                    IniFile.StockerValeur("Configuration", "LANGUAGE", "FRENCH");
                    break;
            }

            if(m_bFirstTime)
                if (System.Windows.MessageBox.Show("Souhaitez vous créer une sauvegarde ?", "Question", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    try
                    {
                        Mouse.OverrideCursor = Cursors.Wait;
                        File.Copy(tbx_path.Text, Environment.CurrentDirectory + @"\HeroPak_Client_Backup.zip", true);
                        Mouse.OverrideCursor = Cursors.Arrow;

                        System.Windows.MessageBox.Show("Une copie de HeroPak_Client.zip a été créee dans " + Environment.CurrentDirectory, "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                    catch (Exception ex)
                    {
                        System.Windows.MessageBox.Show(ex.Message);
                    }
                }

            this.Close();
        }

        private void btn_backup_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Mouse.OverrideCursor = Cursors.Wait;
                File.Copy(tbx_path.Text, Environment.CurrentDirectory + @"\HeroPak_Client_Backup.zip", true);
                Mouse.OverrideCursor = Cursors.Arrow;

                System.Windows.MessageBox.Show("Une copie de HeroPak_Client.zip a été créee dans " + Environment.CurrentDirectory, "Information", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }
        }

        private void btn_restore_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (System.Windows.MessageBox.Show("Attention ceci aura pour effet de supprimer toutes les modifications que vous avez apporté a vos personnages !\n\tVoulez-vous continuer ?", "Attention", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
                {
                    Mouse.OverrideCursor = Cursors.Wait;
                    File.Copy(Environment.CurrentDirectory + @"\HeroPak_Client_Backup.zip", tbx_path.Text, true);
                    Mouse.OverrideCursor = Cursors.Arrow;

                    System.Windows.MessageBox.Show("Votre configuration a été réinitialisé", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                }
           }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }
        }
    }
}
